package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "5") Integer size){
        Page<Item> result=itemService.page(new Page<>(page,size),null);
        for (Item record : result.getRecords()) {
            System.out.println(record);
        }
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setTotal(result.getTotal());
        itemPageDTO.setList(result.getRecords());
        return itemPageDTO;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item selectById(Long id){
        Item item=itemService.getById(id);
        return item;
    }

    /**
     * 保存
     * @param item
     */
    @PostMapping()
    public void save(@RequestBody Item item){
        itemService.save(item);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void shelf(@RequestParam Integer id, @RequestParam Integer status){
        long id1=id;
        Item item = itemService.getById(id1);
        item.setStatus(status);
        itemService.updateById(item);
    }
    /**
     * 修改商品
     */
    @PutMapping()
   public void updateItem(@RequestBody Item item){
        itemService.updateById(item);
    }



}
